package com.shiliu.api.configuration;

import com.shiliu.api.common.filter.TokenApiFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.DispatcherType;

/**
 * @description: web mvc 自定义
 * @author: shili
 * @created: 2019/12/25 21:16
 * @version: v1.0.0
 */
@Configuration
public class WebMvcAdapter implements WebMvcConfigurer {

    /**
     * 配置过滤器注入到spring的容器中
     */
    @Bean
    public FilterRegistrationBean registrationBean(){
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        // 注册token验证拦截器
        registrationBean.setFilter(new TokenApiFilter());
        registrationBean.setDispatcherTypes(DispatcherType.REQUEST);
        // 设置拦截规则
        registrationBean.addUrlPatterns("/api/v1/*");
        // 设置不拦截规则,多个以逗号分隔
        registrationBean.addInitParameter("exclusions","/api/v1/login,/api/v1/register");
        return registrationBean;
    }
}
